// pages/wxaccount_auth/wxaccount_auth.js
var common = require("../../common/common.js").common;

var drawQrcode = require("../../utils/weapp.qrcode.esm");

Page({
    /**
   * 页面的初始数据
   */
    data: {},
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {
        this.getAuthUrl();
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    mainColor: res.extra.color_main
                });
            }
        });
    },
    getAuthUrl() {
        var that = this;
        common.request({
            endpoint: "Wxaccount/manage/auth-url",
            method: "get",
            data: {},
            success: result => {
                let url = result.extra.url;
                const query = wx.createSelectorQuery();
                query.select("#myQrcode").fields({
                    node: true,
                    size: true
                }).exec(res => {
                    var canvas = res[0].node;
                    // 调用方法drawQrcode生成二维码
                                        drawQrcode({
                        canvas: canvas,
                        canvasId: "myQrcode",
                        width: 260,
                        padding: 30,
                        background: "#ffffff",
                        foreground: "#000000",
                        text: url
                    });
                    // 获取临时路径（得到之后，想干嘛就干嘛了）
                                        wx.canvasToTempFilePath({
                        canvasId: "myQrcode",
                        canvas: canvas,
                        x: 0,
                        y: 0,
                        width: 260,
                        height: 260,
                        destWidth: 260,
                        destHeight: 260,
                        success(res) {
                            that.setData({
                                tempFilePath: res.tempFilePath
                            });
                            // console.log('二维码临时路径：', res.tempFilePath)
                                                },
                        fail(res) {
                            console.error(res);
                        }
                    });
                });
            }
        });
    },
    saveLocal() {
        // console.log(this.data.tempFilePath)
        wx.saveImageToPhotosAlbum({
            // 存成图片至手机
            filePath: this.data.tempFilePath,
            success(res2) {
                wx.hideLoading();
                wx.showToast({
                    title: "保存成功",
                    duration: 2e3
                });
            },
            fail(res3) {
                if (res3.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                    wx.showToast({
                        title: "保存失败，稍后再试",
                        duration: 2e3,
                        icon: "none"
                    });
                    wx.hideLoading();
                } else {
                    wx.showToast({
                        title: "保存失败，稍后再试",
                        duration: 2e3,
                        icon: "none"
                    });
                    wx.hideLoading();
                }
            }
        });
    }
});